//! @Author: DengLibin 
//! @Date:  2022-04-21 10:08:43
//! @Description:
use actix_web::{http::header::ContentType, web, Error, HttpResponse, Scope};

use dao::entity::Product;


/// @Author: DengLibin 
/// @Date:  2022-04-21 11:33:01
/// @Description: 路由配置
pub fn route() -> Scope{
    let prodcuts_scope = web::scope("products");
    let r = web::resource("")
                    .route(web::get().to(get_products))
                    .route(web::post().to(add_product));
    let r2 =  web::resource("/{product_id}")
                        .route(web::get().to(get_product_detail))
                        .route(web::delete().to(remove_product));   
                                    
    prodcuts_scope.service(r).service(r2)
}


pub async fn get_products() -> Result<HttpResponse, Error> {
    Ok(HttpResponse::Ok()
        .content_type(ContentType::plaintext())
        .body("get_products"))
}

pub async fn add_product(_new_product: web::Json<Product>) -> Result<HttpResponse, Error> {
    Ok(HttpResponse::Ok().finish())
}

pub async fn get_product_detail(id: web::Path<String>) -> Result<HttpResponse, Error> {
    Ok(HttpResponse::Ok()
        .content_type(ContentType::plaintext())
        .body(format!("id:{}",id.into_inner())))
}

pub async fn remove_product(_id: web::Path<String>) -> Result<HttpResponse, Error> {
    Ok(HttpResponse::Ok().finish())
}
